<template>
  <view class="container" style="padding-bottom: 90px">
    <!--  tab 栏 -->
    <view class="wh100">
      <view class="data-empty-wh100" v-if="pageList == null || pageList.length == 0">
        <u-empty :text="`${subject}为空~~`" />
      </view>
      <view v-else class="items-container" style="padding-bottom: 90px">
        <view @click="onItemClick(item)" class="items" :id="`menu-${item.id}`" v-for="item of pageList" :key="item.id">
          <text class="item-title">{{ item[itemName] == null ? '无' : item[itemName] }}</text>
          <view>
            <text
              :style="{
                marginRight: `5px`,
                color: `rgb(243, 196, 93)`
              }"
              style="font-size: 26rpx"
              v-on:click.stop="operate($event, item, 1)"
            >
              重置密码
            </text>
          </view>
        </view>
        <!-- 加载更多 -->
        <li-loadmore :status="loadStatus" :loading-text="loadingText" :loadmore-text="loadmoreText" :nomore-text="nomoreText" @loadmore="onReachBottomEvent" />
      </view>
    </view>
    <view class="bottom-add" @click="btnAddClick">新建{{ subject }}</view>
  </view>
</template>

<script>
import { uShowLoading, uHideLoading, utoast, uNavigateTo, uSwitchTab, uSetNavigationBarTitle, umodal } from '../../../utils/uniUtils.js';

import liListMixin from '../../../mixins/liListMixin';
import { GetSysUserList, ResetPassword } from '../../../common/modules/m.js';

export default {
  mixins: [liListMixin],
  data() {
    return {
      itemName: 'nickName',
      subject: '用户',
      activeStyle: {
        color: '#F3C45D'
      },
      tabMenusIdx: 0,
      tabMenus: null,
      leftMenus: null,
      leftMenusIdx: null,
      pageList: [],
      rootRoute: 'workbench',
      routeName: 'user',
      pageListFn: GetSysUserList,
      isLoaded: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      showLoadMore: false,
      loadStatus: 'loadmore',
      loadingText: '努力加载中。。。',
      loadmoreText: '上拉加载更多~~',
      nomoreText: '实在没有了'
    };
  },
  onReachBottom() {
    this.onReachBottomEvent();
  },
  async mounted() {
    try {
      uSetNavigationBarTitle('用户管理');
      uShowLoading();
      await this.GetPageList();
      this.isLoaded = true;
    } catch (error) {
      console.log('err', error);
      utoast('初始化失败！', 'error');
      setTimeout(() => {
        uSwitchTab('/pages/tabBar/staging');
      }, 800);
    } finally {
      uHideLoading();
    }
  },
  methods: {
    onReachBottomEvent() {
      this.reachBtm(this);
    },

    async onTabClick(e) {
      this.onTabClickEvent(e, this);
    },

    async GetPageList(idx = 0) {
      console.log('GetPageList');

      // console.log("GetFoodFeed");
      // console.log("this.tabMenus[idx] ", this.tabMenus[idx]);

      //  有  tabMenu时放开
      //   if (this.tabMenus[idx] != null) {
      if (this.showLoadMore) {
        this.pageNum += 1;
      }

      this.loadStatus = 'loading';

      let res = await this.pageListFn({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        deleted: false
        // storeId: storeId,
      });
      let list = res.data?.list || res.data;
      this.total = res.data?.total || res.count;
      this.pageList = [...this.pageList, ...list];

      if (this.pageList.length == this.total) {
        this.loadStatus = 'nomore';
      } else {
        this.loadStatus = 'loadmore';
      }
      this.showLoadMore = false;
      // console.log("tagList", list);
      //   }
    },

    async btnAddClick() {
      this.btnAddClickEvent(this);
    },

    onItemClick(item) {
      this.onItemClickEvent(item, this);
    },

    operate: async function (e, item, type) {
      e.stopPropagation();
      const fn = type == 1 ? ResetPassword : ResetPassword;
      if (await umodal('确定要重置密码吗？')) {
        const { code, msg } = await fn({ id: item.id });
        if (code == 1) {
          utoast(msg);
        } else {
          await this.resetList();
          utoast('重置成功！');
        }
      }
    }

    // methods put on the top
  }
};
</script>

<style lang="scss" scoped>
@import '~@/static/styles/commonStyle.scss';
.container {
  height: 100.1vh;
}
</style>
